using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class UInstrumentControl : UPanel
    {
        private double value = 0;
        private double minValue = 0;
        private double maxValue = 300;
        private string unit = "V";
        private string title = "电压";
        private double startAngle = 135;
        private double sweepAngle = 270;
        private int scaleCount = 10;
        private Color arrowColor = Color.Red;
        private UArrowControl arrowControl;

        [Category("数据")]
        [Description("当前值")]
        public double Value
        {
            get { return value; }
            set 
            {
                this.value = Math.Max(minValue, Math.Min(maxValue, value));
                if (arrowControl != null)
                {
                    arrowControl.Value = this.value;
                }
                this.Invalidate();
            }
        }

        [Category("数据")]
        [Description("最小值")]
        public double MinValue
        {
            get { return minValue; }
            set 
            {
                minValue = value;
                if (arrowControl != null)
                {
                    arrowControl.MinValue = minValue;
                }
                this.Invalidate();
            }
        }

        [Category("数据")]
        [Description("最大值")]
        public double MaxValue
        {
            get { return maxValue; }
            set 
            {
                maxValue = value;
                if (arrowControl != null)
                {
                    arrowControl.MaxValue = maxValue;
                }
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("单位")]
        public string Unit
        {
            get { return unit; }
            set 
            {
                unit = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("标题")]
        public string Title
        {
            get { return title; }
            set 
            {
                title = value;
                this.Invalidate();
            }
        }

        public UInstrumentControl()
        {
            this.BackColor = Color.White;
            this.CornerRadius = 10;
            this.BorderColor = Color.DarkBlue;
            this.Size = new Size(100, 100);

            arrowControl = new UArrowControl();
            arrowControl.Dock = DockStyle.Fill;
            arrowControl.MinValue = minValue;
            arrowControl.MaxValue = maxValue;
            arrowControl.StartAngle = startAngle;
            arrowControl.SweepAngle = sweepAngle;
            arrowControl.ArrowColor = arrowColor;
            this.Controls.Add(arrowControl);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            int centerX = this.Width / 2;
            int centerY = this.Height / 2;
            int radius = Math.Min(centerX, centerY) - 20;

            // 绘制刻度盘
            using (Pen pen = new Pen(Color.Blue, 1))
            {
                g.DrawEllipse(pen, centerX - radius, centerY - radius, radius * 2, radius * 2);
            }

            // 绘制刻度
            using (Pen pen = new Pen(Color.Black, 1))
            {
                for (int i = 0; i <= scaleCount; i++)
                {
                    double angle = (startAngle + (sweepAngle * i / scaleCount)) * Math.PI / 180;
                    int x1 = centerX + (int)(Math.Cos(angle) * radius * 0.8);
                    int y1 = centerY - (int)(Math.Sin(angle) * radius * 0.8);
                    int x2 = centerX + (int)(Math.Cos(angle) * radius);
                    int y2 = centerY - (int)(Math.Sin(angle) * radius);
                    g.DrawLine(pen, x1, y1, x2, y2);
                }
            }

            // 绘制刻度值
            using (Font font = new Font("Arial", 6F))
            {
                using (Brush brush = new SolidBrush(Color.Black))
                {
                    for (int i = 0; i <= scaleCount; i++)
                    {
                        double angle = (startAngle + (sweepAngle * i / scaleCount)) * Math.PI / 180;
                        double scaleValue = minValue + (maxValue - minValue) * i / scaleCount;
                        string scaleText = scaleValue.ToString("0");
                        int x = centerX + (int)(Math.Cos(angle) * radius * 1.2);
                        int y = centerY - (int)(Math.Sin(angle) * radius * 1.2);
                        SizeF textSize = g.MeasureString(scaleText, font);
                        g.DrawString(scaleText, font, brush, x - textSize.Width / 2, y - textSize.Height / 2);
                    }
                }
            }

            // 绘制标题
            using (Font font = new Font("微软雅黑", 8F, FontStyle.Bold))
            {
                using (Brush brush = new SolidBrush(Color.Black))
                {
                    SizeF textSize = g.MeasureString(title, font);
                    g.DrawString(title, font, brush, centerX - textSize.Width / 2, 5);
                }
            }

            // 绘制当前值
            using (Font font = new Font("微软雅黑", 9F, FontStyle.Bold))
            {
                using (Brush brush = new SolidBrush(Color.Red))
                {
                    string valueText = value.ToString("0") + unit;
                    SizeF textSize = g.MeasureString(valueText, font);
                    g.DrawString(valueText, font, brush, centerX - textSize.Width / 2, centerY + radius * 0.5);
                }
            }
        }
    }
}